package com.qigao.mall.channel.alipay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qigao.mall.channel.ChannelCore;
import com.qigao.mall.channel.dto.ChannelAuthRespDto;
import com.qigao.mall.commons.dto.MerChannelDto;
import com.qigao.mall.commons.enums.ChannelActionEnum;
import com.qigao.mall.commons.enums.ChannelCodeEnum;
import com.qigao.mall.commons.enums.ChannelInfoEnum;
import com.qigao.mall.commons.enums.PayProductEnum;
import com.qigao.mall.commons.param.ChannelReqParam;
import com.qigao.mall.commons.param.ChannelRespParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 微信小程序预登录
 */
@Service
public class AliPayAuthToken extends AbstractAliPay implements ChannelCore {
    private static Logger logger= LoggerFactory.getLogger(AliPayAuthToken.class);
    @Override
    public boolean isSupport(PayProductEnum payProductEnum, ChannelActionEnum channelActionEnum, ChannelInfoEnum channelInfoEnum) {
        return ChannelActionEnum.CHECK_TOKEN.equals(channelActionEnum) &&
                PayProductEnum.ALIPAY_PC_WEB.equals(payProductEnum)&&
                ChannelInfoEnum.ALIPAY.equals(channelInfoEnum)
                ;
    }

    @Override
    protected String getTradeMethod() {
        return "alipay.open.auth.token.app";
    }

    @Override
    public Map buildSelfReqParam(MerChannelDto merChannelDto, ChannelReqParam channelReqParam) {
        Map reqParam=new ConcurrentHashMap<>();
        JSONObject bizContent=new JSONObject();
        bizContent.put("grant_type", "authorization_code");
        bizContent.put("code", channelReqParam.getReqContent());
        reqParam.put("biz_content",bizContent.toString());
        return reqParam;
    }

    @Override
    protected ChannelRespParam buildSelfRespData(JSONObject respJson, MerChannelDto merChannelDto) {
        ChannelRespParam channelRespParam=new ChannelRespParam();
        channelRespParam.setRespCode(ChannelCodeEnum.SUCCESS.getCode());
        channelRespParam.setRespMsg(ChannelCodeEnum.SUCCESS.getName());
        ChannelAuthRespDto channelAuthRespDto=new ChannelAuthRespDto();
        channelAuthRespDto.setUserId(respJson.getString("user_id"));
        channelAuthRespDto.setAuthAppId(respJson.getString("auth_app_id"));
        channelAuthRespDto.setAppAuthToken(respJson.getString("app_auth_token"));
        channelAuthRespDto.setAppRefreshToken(respJson.getString("app_refresh_token"));
        channelAuthRespDto.setExpiresIn(respJson.getString("expires_in"));
        channelAuthRespDto.setReExpiresIn(respJson.getString("re_expires_in"));
        channelRespParam.setRespContent(JSON.toJSONString(channelAuthRespDto));
        return channelRespParam;
    }




}
